package com.sugon.controller.basics;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.FileTypeEntity;
import com.sugon.service.FileTypeService;
import com.sugon.utils.Constant;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备类型Controller
 *
 * @author sugon
 * @date 2019-07-11 09:18:25
 */
@Controller
@RequestMapping("filetype")
public class FileTypeController {
    @Autowired
    private FileTypeService fileTypeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
//       Query query = new Query(params);

       /* List<FileTypeEntity> fileTypeList = fileTypeService.queryList(query);
        int total = fileTypeService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(fileTypeList, total, query.getLimit(), query.getPage());*/
//        Map<String, Object> map = new HashMap<>();
        List<FileTypeEntity> fileTypeList = fileTypeService.queryList(params);
        return R.ok().put("list", fileTypeList);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        FileTypeEntity fileType = fileTypeService.queryObject(id);

        return R.ok().put("fileType", fileType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("filetype:save")
    @ResponseBody
    public R save(@RequestBody FileTypeEntity fileType) {
        fileTypeService.save(fileType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("filetype:update")
    @ResponseBody
    public R update(@RequestBody FileTypeEntity fileType) {
        fileTypeService.update(fileType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{ids}")
    @RequiresPermissions("filetype:delete")
    @ResponseBody
    public R delete(@PathVariable String ids) {
        FileTypeEntity fileType = fileTypeService.queryObject(ids);
        //判断是否有下级
        List<FileTypeEntity> list = fileTypeService.queryPidList(fileType.getId(),null);
        if (list.size() > 0) {
            return R.error("请先删除子资料类型");
        }
        fileType.setDelFlag(Integer.valueOf(Constant.IS_DEL));
        fileTypeService.update(fileType);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<FileTypeEntity> list = fileTypeService.queryList(params);

        return R.ok().put("list", list);
    }
    /**
     * 选择故障类型
     */
    @RequestMapping("/select")
    @ResponseBody
    public R select(){
        Map<String, Object> map = new HashMap<>();

        List<FileTypeEntity> fileTypeList = fileTypeService.queryList(map);

        return R.ok().put("fileTypeList", fileTypeList);
    }
}
